#include "main.h"
#include "ADC.h"





void initADC(void)
{
    PINSEL1 &= ~(PINSEL1_P0p29_MASK);
    PINSEL1 |= PINSEL1_P0p29_ADC0p2;  // AD0.2
    PCONP |= PCONP_ADC0_PWR;   //adc0 power
    AD0CR = AD_CR_PDN_ON | AD_CR_CLK_DIV_11 | AD0CR_SEL_AD0p2;

    PINSEL1 &= ~(PINSEL1_P0p22_MASK);
    PINSEL1 |= PINSEL1_P0p22_ADC1p7; // AD1.7    
    PCONP |= PCONP_ADC1_PWR;   //adc1 power
    AD1CR = AD_CR_PDN_ON | AD_CR_CLK_DIV_11 | AD1CR_SEL_AD1p7;  //select ad1.7 for bat-cap
}
void adc_init(volatile unsigned long * pinsel, volatile unsigned long * adxcr, unsigned long pinsel_pxy_mask, unsigned long pinsel_pxy_fxn, unsigned long pconp_sel, unsigned long ADxCR_mask)
{
  *(pinsel) &= ~(pinsel_pxy_mask);
  *(pinsel) |= pinsel_pxy_fxn;
  PCONP     |= pconp_sel;
  *(adxcr)  |= ADxCR_mask;
}

inline int getADC0(void)
{
    int _ch3,_ch2,_ch1,max;
    
    AD0CR |= (1<<2);
    AD0CR |= (1<<24);   // initiate ADC start
    while (!(AD0DR2 & (1<<31)));    // wait for the ADC to finish
    _ch1 = (AD0DR2>>6)&0x3FF;
    AD0CR &= ~(1<<2);   // turn off ADC channel
/*    
    
    AD0CR |= (1<<2);
    AD0CR |= (1<<24);   // initiate ADC start
    while (!(AD0DR2 & (1<<31)));    // wait for the ADC to finish
    _ch2 = (AD0DR2>>6)&0x3FF;
    AD0CR &= ~(1<<2);   // turn off ADC channel
  
    AD0CR |= (1<<3);
    AD0CR |= (1<<24);   // initiate ADC start
    while (!(AD0DR3 & (1<<31)));    // wait for the ADC to finish
    _ch3 = (AD0DR3>>6)&0x3FF;
    AD0CR &= ~(1<<3);   // turn off ADC channel
 ///* 
    max = (_ch1>_ch2) ? _ch1:_ch2;
    max = (max<_ch3) ? _ch3:max;

    return max;
   */
    
    return _ch1;
}
int adc_get(volatile unsigned long * adxcr, unsigned long ad_cr_sel_val, volatile unsigned long * adxdr_val)
{
  *(adxcr)  &=  ~(AD_CR_START_MASK | AD_CR_SELMASK);
  *(adxcr)  |=  AD_CR_START_NONE | ad_cr_sel_val;
  *(adxcr)  |= AD_CR_START_NOW;
  volatile unsigned long dataReg;
  do {
      dataReg = (unsigned long) adxdr_val;
  } while ((dataReg & AD_DR_DONE) == 0);
  return (*(adxdr_val) & AD_DR_RESULT_MASK)>>AD_DR_RESULTSHIFT;
}
inline int getADC1(void)
{
//  #define AD_CR_START_MASK
//  #define AD_CR_SELMASK
//  #define AD_CR_START_NOW
//  #define AD_CR_SEL7

    int _ch3,_ch2,_ch1,max;

//      AD0_CR &= ~(AD_CR_START_MASK | AD_CR_SELMASK);
//  AD0_CR |=  (AD_CR_START_NOW | AD_CR_SEL3);
//  AD0_CR |=   AD_CR_START_NOW;

    AD1CR &=  ~(AD_CR_START_MASK | AD_CR_SELMASK);
    AD1CR |=  AD_CR_START_NONE | AD_CR_SEL7;  // initiate ADC start
    AD1CR |= AD_CR_START_NOW;
    volatile unsigned int dataReg;
    do {
      dataReg = AD1DR7;
    } while ((dataReg & AD_DR_DONE) == 0);
//    while (!(AD1DR7 & AD_DR_DONE))
//    {
//      P0SET =(1<<11)|(1<<30);    // wait for the ADC to finish
//    }
//    _ch1 = (AD1DR7>>6)&0x3FF;
     _ch1 = (AD1DR7 & AD_DR_RESULT_MASK)>>AD_DR_RESULTSHIFT;
//    AD1CR &= ~(1<<7);   // turn off ADC channel
/*    
    
    AD0CR |= (1<<2);
    AD0CR |= (1<<24);   // initiate ADC start
    while (!(AD0DR2 & (1<<31)));    // wait for the ADC to finish
    _ch2 = (AD0DR2>>6)&0x3FF;
    AD0CR &= ~(1<<2);   // turn off ADC channel
  
    AD0CR |= (1<<3);
    AD0CR |= (1<<24);   // initiate ADC start
    while (!(AD0DR3 & (1<<31)));    // wait for the ADC to finish
    _ch3 = (AD0DR3>>6)&0x3FF;
    AD0CR &= ~(1<<3);   // turn off ADC channel
 ///* 
    max = (_ch1>_ch2) ? _ch1:_ch2;
    max = (max<_ch3) ? _ch3:max;

    return max;
   */
    
    return _ch1;
}